function doMove(obj, attr, dir, target, endFn) {
    //判断数值的正负，如果目标值大于当前值则为正
    dir = parseFloat(getStyle(obj, attr)) < target ? dir : -dir;

    clearInterval(obj.timer);

    obj.timer = setInterval(function () {
        //下一个位置的变化
        var speed = parseFloat(getStyle(obj, attr)) + dir;
        //超过目标值时，speed改为目标值
        if (speed > target && dir > 0 || speed < target && dir < 0) {
            speed = target;
        };
        obj.style[attr] = speed + 'px';
        if (speed == target) {
            clearInterval(obj.timer);
            endFn && endFn();
        };
    }, 20);
};

function chOpacity(obj, speed, target, endFn) {
    //取得最初值,要用parseInt不然得到的opacity属性是字符串
    var oropacity = parseInt(getStyle(obj, 'opacity'));

    //定时器
    obj.chopacity = null;
    //判断正负
    speed = oropacity > target ? -speed : speed;

    clearInterval(obj.chopacity);

    obj.chopacity = setInterval(function () {

        obj.style.opacity = oropacity += speed;

        if (obj.style.opacity < 0) {
            obj.style.opacity = 0;
            clearInterval(obj.chopacity);
            endFn && endFn();

        }
        if (obj.style.opacity > 1) {
            obj.style.opacity = 1;
            clearInterval(obj.chopacity);
            endFn && endFn();
        }
    }, 40);

}

function getStyle(obj, attr) {
    return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj)[attr];
};

// 使元素左右抖动的方法
function doShake(dom) {
    var rang = []
    // [20,-20,18,-18...0]
    for (let i = 20; i > 0; i -= 2) {
        rang.push(i, -i)
    }
    rang.push(0)
    clearInterval(dom.shake)
    // 短时间内反复左右移动，形成抖动的效果
    var i = 0
    let left = getIntStyle(dom, 'left')
    dom.shake = setInterval(() => {
        if (i > 20)
            clearInterval(dom.shake)
        else {
            left += rang[i++]
            dom.style.setProperty('left', left + 'px');
        }
    }, 70)
}
// 将某些css属性以number形式返回
function getIntStyle(dom, attr) {
    let strAttr = getStyle(dom, attr)
    if (strAttr.indexOf('px') != -1)
        return parseFloat(strAttr.substring(0, strAttr.indexOf('px')));
    else
        return strAttr;
}